﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.Mvvm;
using VisaCRM.Modules.Admin.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using VisaCRM.Modules.Admin.Gateway;
using VisaCRM.Modules.Admin.ViewModels.Helper;

namespace VisaCRM.Modules.Admin.ViewModels
{
    /// <summary>
    /// ViewModel of an list of Product Types
    /// </summary>
    class ProductTypesViewModel:BindableBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
         private  ObservableCollection<ProductTypes> allProductTypes { get; set; }
         public  ObservableCollection<Country> AvailableDestination { get; set; }
         public ObservableCollection<Department> AvailableDepartment { get; set; }
         public ObservableCollection<StageDiagram> AvailableStageDiagram { get; set; }
         public ObservableCollection<ChargeGroup> AvailableChargeGroup { get; set; }
         public ObservableCollection<EventGroup> AvailableEventGroup { get; set; }
         public ObservableCollection<Branch> AvailableBranch { get; set; }
         public ObservableCollection<Country> AvailableResidence { get; set; }

        /// <summary>
        /// initialise gateway used
        /// </summary>
         ProductTypesGateway producttypeGateway = new ProductTypesGateway();
         CountryGateway countryGateway = new CountryGateway();
         DepartmentGateway departmentGateway = new DepartmentGateway();
         BranchGateway branchGateway = new BranchGateway();

        ///summary
        ///initialises a new instance of ProductTypesViewmodel class
       ///summary
        public ProductTypesViewModel(ObservableCollection<ProductTypes> allproducttypes)
       {
           currentProductType = new ProductTypes();
           if (allproducttypes == null || allproducttypes.Count == 0)
           {

               AllProductTypes = producttypeGateway.GetAllproducttypes();
           }
           else
           {
               this.AllProductTypes = allproducttypes;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllProductTypes.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentProductType))
               {
                   this.CurrentProductType = null;
               }
           };

           this.AvailableDestination = countryGateway.GetAllcountries();
           this.AvailableDepartment = departmentGateway.GetAlldeparments();
           this.AvailableStageDiagram = producttypeGateway.GetAllStageDiagram();
           this.AvailableChargeGroup = producttypeGateway.GetAllChargeGroups();
           this.AvailableEventGroup = producttypeGateway.GetAllEventgroup();
           this.AvailableBranch = branchGateway.GetAllBranch();
           this.AvailableResidence = countryGateway.GetAllcountries();

           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.DeleteCommand = new DelegateCommand((o) => this.Delete());
           this.EditCommand = new DelegateCommand((o) => this.Edit());
       }

       public ICommand AddNewCommand { get; private set; }
       public ICommand SaveCommand { get; private set; }
       public ICommand CancelCommand { get; private set; }
       public ICommand DeleteCommand { get; private set; }
       public ICommand EditCommand { get; private set; }

       private void Save()
       {
           if (this.CurrentProductType != null)
           {
               ProductTypes productTypes = new ProductTypes() 
               { ProductTypeName = this.CurrentProductName,
                   ProductTypeBranchID=this.CurrentBranch.BranchID,
                   ProductTypeChargeGroupID=this.CurrentChargeGroup.ChargeGroupID,
                   ProductTypeDepartmentID=this.CurrentDepartment.DepartmentID,
                   ProductTypeDestinationID=Convert.ToInt32( this.CurrentDestination.CountryId),
                   ProductTypeEventGroupID=this.CurrentEventGroup.EventGroupID,
                 ProductTypeResidenceID = Convert.ToInt32(this.CurrentResidence.CountryId),
                   ProductTypeStageDiagramID=this.CurrentStageDiagram.StageDiagramID
               
               };
               bool flag = false;
               if (string.IsNullOrEmpty(productTypes.ProductTypeName))
                   return;
               foreach (ProductTypes pt in this.AllProductTypes)
               {
                   if (pt.ProductTypeName == productTypes.ProductTypeName)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The product type details are already listed");
                   return;
               }
               producttypeGateway.SaveNewProductTypeToDatabase(productTypes.ProductTypeName,productTypes.ProductTypeDestinationID,productTypes.ProductTypeDepartmentID,productTypes.ProductTypeStageDiagramID,productTypes.ProductTypeChargeGroupID,productTypes.ProductTypeEventGroupID,productTypes.ProductTypeBranchID,productTypes.ProductTypeResidenceID);
               AddNew();
               this.AllProductTypes = producttypeGateway.GetAllproducttypes();
               System.Windows.Forms.MessageBox.Show("The Product Type details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill Product Type details");
           }
       }

       private void AddNew()
       {
           this.CurrentStageDiagram = null;
           this.CurrentDestination = null;
           this.CurrentChargeGroup = null;
           this.CurrentBranch = null;
           this.CurrentDepartment = null;
           this.CurrentResidence = null;
           this.CurrentEventGroup = null;
           this.CurrentProductName = null;  
       }
       private void Cancel()
       {
           this.currentProductType = null;
       }
       private void Delete()
       {
           if (this.CurrentProductType != null)
           {
               producttypeGateway.DeleteProductTypeFromDatabase(this.CurrentProductType.ProductTypeName);
               AllProductTypes.Remove(this.CurrentProductType);
               AddNew();
               System.Windows.Forms.MessageBox.Show("The Product Types details is deleted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please select Product Types details to be deleted");
           }
         
       }

       private void Edit()
       {
           if (this.CurrentProductType != null)
           {
               ProductTypes productTypes = new ProductTypes()
               {
                   ProductTypeName = this.CurrentProductType.ProductTypeName,
                   ProductTypeBranchID = this.CurrentBranch.BranchID,
                   ProductTypeChargeGroupID = this.CurrentChargeGroup.ChargeGroupID,
                   ProductTypeDepartmentID = this.CurrentDepartment.DepartmentID,
                   ProductTypeDestinationID = Convert.ToInt32(this.CurrentDestination.CountryId),
                   ProductTypeEventGroupID = this.CurrentEventGroup.EventGroupID,
                   ProductTypeResidenceID = Convert.ToInt32(this.CurrentResidence.CountryId),
                   ProductTypeStageDiagramID = this.CurrentStageDiagram.StageDiagramID,
                   ProductTypeID=this.CurrentProductType.ProductTypeID

               };
               this.CurrentProductType = productTypes;
               producttypeGateway.EditProductTypeToDatabase(productTypes.ProductTypeID, productTypes.ProductTypeName, productTypes.ProductTypeDestinationID, productTypes.ProductTypeDepartmentID, productTypes.ProductTypeStageDiagramID, productTypes.ProductTypeChargeGroupID, productTypes.ProductTypeEventGroupID, productTypes.ProductTypeBranchID, productTypes.ProductTypeResidenceID);
               this.AllProductTypes = producttypeGateway.GetAllproducttypes();
               this.CurrentProductType = productTypes;
               System.Windows.Forms.MessageBox.Show("The Product Types details is editted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please select Product Types details to be edited");
           }
       }

        

       /// <summary>
       /// The Product Type currently selected in the workspace
       /// </summary>
       private ProductTypes  currentProductType;

       /// <summary>
       /// Gets or sets the ProductTypes currently selected in the workspace
       /// </summary>
       public ProductTypes CurrentProductType
       {
           get
           {
               return this.currentProductType;
           }

           set
           {
               this.currentProductType = value;
               if (this.CurrentProductType != null)
               {
                       this.CurrentProductName = this.CurrentProductType.ProductTypeName;
                       this.CurrentDestination = AvailableDestination.FirstOrDefault(des => des.CountryName == (producttypeGateway.GetCurrentDestination(this.currentProductType.ProductTypeDestinationID)).CountryName);
                       this.CurrentDepartment = AvailableDepartment.FirstOrDefault(dep => dep.DepartmentName == (departmentGateway.GetCurrentDepartment(this.currentProductType.ProductTypeDepartmentID)).DepartmentName);
                       this.CurrentStageDiagram = AvailableStageDiagram.FirstOrDefault(std => std.StageDiagramName == (producttypeGateway.GetCurrentStagediagram(this.currentProductType.ProductTypeStageDiagramID)).StageDiagramName);
                       this.CurrentChargeGroup = AvailableChargeGroup.FirstOrDefault(cg => cg.ChargeGroupName == (producttypeGateway.GetCurrentChargeGroup(this.currentProductType.ProductTypeChargeGroupID)).ChargeGroupName);
                       this.CurrentBranch = AvailableBranch.FirstOrDefault(b => b.BranchName == (branchGateway.GetCurrentbranch(this.currentProductType.ProductTypeBranchID)).BranchName);
                       this.CurrentEventGroup = AvailableEventGroup.FirstOrDefault(eg => eg.EventGroupName == (producttypeGateway.GetCurrentEventGroup(this.currentProductType.ProductTypeEventGroupID)).EventGroupName);
                       this.CurrentResidence = AvailableResidence.FirstOrDefault(res => res.CountryName == (producttypeGateway.GetCurrentDestination(this.currentProductType.ProductTypeResidenceID)).CountryName);
               }
               this.OnPropertyChanged("CurrentProductType");
           }
       }


       
       /// <summary>
       /// Gets or sets the product type currently selected in the workspace
       /// </summary>
       public ObservableCollection<ProductTypes> AllProductTypes
       {
           get
           {
               return this.allProductTypes;
           }

           set
           {
               this.allProductTypes = value;
               this.OnPropertyChanged("AllProductTypes");
           }
       }

        /// <summary>
        /// the product name currently selected in the workspace
        /// </summary>
       private string currentProductName;
       /// <summary>
       /// Gets or sets the productname currently selected in the workspace
       /// </summary>
       public string CurrentProductName
       {
           get
           {
               return this.currentProductName;
           }

           set
           {
               this.currentProductName = value;
               this.OnPropertyChanged("CurrentProductName");
           }
       }

       /// <summary>
       /// The Destination currently selected in the workspace
       /// </summary>
       private Country currentDestination;

       /// <summary>
       /// Gets or sets the Destination currently selected in the workspace
       /// </summary>
       public Country CurrentDestination
       {
           get
           {
               return this.currentDestination;
           }

           set
           {
               this.currentDestination = value;
               this.OnPropertyChanged("CurrentDestination");
           }
       }

       /// <summary>
       /// The Residence currently selected in the workspace
       /// </summary>
       private Country currentResidence;

       /// <summary>
       /// Gets or sets the Residence currently selected in the workspace
       /// </summary>
       public Country CurrentResidence
       {
           get
           {
               return this.currentResidence;
           }

           set
           {
               this.currentResidence = value;
               this.OnPropertyChanged("CurrentResidence");
           }
       }

       /// <summary>
       /// The depatment currently selected in the workspace
       /// </summary>
       private Department currentDepartment;

       /// <summary>
       /// Gets or sets the depatment currently selected in the workspace
       /// </summary>
       public Department CurrentDepartment
       {
           get
           {
               return this.currentDepartment;
           }

           set
           {
               this.currentDepartment = value;
               this.OnPropertyChanged("CurrentDepartment");
           }
       }

       /// <summary>
       /// The Stage diagram currently selected in the workspace
       /// </summary>
       private StageDiagram currentStageDiagram;

       /// <summary>
       /// Gets or sets the Stage diagram currently selected in the workspace
       /// </summary>
       public StageDiagram CurrentStageDiagram
       {
           get
           {
               return this.currentStageDiagram;
           }

           set
           {
               this.currentStageDiagram = value;
               this.OnPropertyChanged("CurrentStageDiagram");
           }
       }

       /// <summary>
       /// The Charge Group currently selected in the workspace
       /// </summary>
       private ChargeGroup currentChargeGroup;

       /// <summary>
       /// Gets or sets the Charge Group currently selected in the workspace
       /// </summary>
       public ChargeGroup CurrentChargeGroup
       {
           get
           {
               return this.currentChargeGroup;
           }

           set
           {
               this.currentChargeGroup = value;
               this.OnPropertyChanged("CurrentChargeGroup");
           }
       }

       /// <summary>
       /// The EventGroup currently selected in the workspace
       /// </summary>
       private EventGroup currentEventGroup;

       /// <summary>
       /// Gets or sets the EventGroup currently selected in the workspace
       /// </summary>
       public EventGroup CurrentEventGroup
       {
           get
           {
               return this.currentEventGroup;
           }

           set
           {
               this.currentEventGroup = value;
               this.OnPropertyChanged("CurrentEventGroup");
           }
       }

       /// <summary>
       /// The Branch currently selected in the workspace
       /// </summary>
       private Branch currentBranch;

       /// <summary>
       /// Gets or sets the Branch currently selected in the workspace
       /// </summary>
       public Branch CurrentBranch
       {
           get
           {
               return this.currentBranch;
           }

           set
           {
               this.currentBranch = value;
               this.OnPropertyChanged("CurrentBranch");
           }
       }
    }
}
